﻿/* **********************************************************************************
 * Copyright (c) 2011 John Hughes
 *
 * Elve Touch is licenced under the Microsoft Reciprocal License (Ms-RL).
 *
 * Project Website: http://elvetouch.codeplex.com/
 * **********************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ElveTouch
{
    // The ImageSizeMode enum is the same as the System.Windows.Forms.PictureBoxSizeMode enum,
    // except it also works in the compact framework
    public enum ImageSizeMode
    {
        /// <summary>
        /// The image is placed in the upper-left corner of the System.Windows.Forms.PictureBox.
        /// The image is clipped if it is larger than the System.Windows.Forms.PictureBox
        /// it is contained in.
        /// </summary>
        Normal = 0,
        /// <summary>
        /// The image within the System.Windows.Forms.PictureBox is stretched or shrunk
        /// to fit the size of the System.Windows.Forms.PictureBox.
        /// </summary>
        StretchImage = 1,
        /// <summary>
        /// The System.Windows.Forms.PictureBox is sized equal to the size of the image
        /// that it contains. This means that the control's actual bounds are changed to
        /// be equal to the size of the image.
        /// </summary>
        AutoSize = 2,
        /// <summary>
        /// The image is displayed in the center if the System.Windows.Forms.PictureBox
        /// is larger than the image. If the image is larger than the System.Windows.Forms.PictureBox,
        /// the picture is placed in the center of the System.Windows.Forms.PictureBox
        /// and the outside edges are clipped.
        /// </summary>
        CenterImage = 3,
        /// <summary>
        /// The size of the image is increased or decreased maintaining the size ratio.
        /// </summary>
        Zoom = 4,
    }

    public class ImageSizeModeHelper
    {
        public static PictureBoxSizeMode ImageSizeModeToPictureBoxSizeMode(ImageSizeMode imgSizeMode)
        {
            switch (imgSizeMode)
            {
                case ImageSizeMode.Normal:
                    return PictureBoxSizeMode.Normal;
                case ImageSizeMode.StretchImage:
                    return PictureBoxSizeMode.StretchImage;
                case ImageSizeMode.AutoSize:
                    return PictureBoxSizeMode.AutoSize;
                case ImageSizeMode.CenterImage:
                    return PictureBoxSizeMode.CenterImage;
                case ImageSizeMode.Zoom:
                    return PictureBoxSizeMode.Zoom;
                default:
                    return PictureBoxSizeMode.Normal;
            }
        }


        public static ImageSizeMode PictureBoxSizeModeToImageSizeMode(PictureBoxSizeMode pbSizeMode)
        {
            switch (pbSizeMode)
            {
                case PictureBoxSizeMode.Normal:
                    return ImageSizeMode.Normal;
                case PictureBoxSizeMode.StretchImage:
                    return ImageSizeMode.StretchImage;
                case PictureBoxSizeMode.CenterImage:
                    return ImageSizeMode.CenterImage;
                case PictureBoxSizeMode.AutoSize:
                    return ImageSizeMode.AutoSize;
                case PictureBoxSizeMode.Zoom:
                    return ImageSizeMode.Zoom;
                default:
                    return ImageSizeMode.Normal;
            }
        }

        public static void ImageRectangleFromSizeMode(Image image, Rectangle clientRectangle, ImageSizeMode mode, out Rectangle sourceRect, out Rectangle destRect)
        {
            // Based on code from Reflector PictureBox code... then I made it better to fix my needs.


            if (image == null)
            {
                sourceRect = Rectangle.Empty;
                destRect = clientRectangle;
                return;
            }

            sourceRect = new Rectangle(0, 0, image.Width, image.Height);
            destRect = clientRectangle;

            switch (mode)
            {
                case ImageSizeMode.Normal: // image is clipped if larger than control
                case ImageSizeMode.AutoSize: // the control's bounds were changed to be the same size as the image.
                    destRect.Size = image.Size;
                    destRect.Intersect(clientRectangle); // make sure the destination is no bigger than the control.

                    if (sourceRect.Width > destRect.Width)
                        sourceRect.Width = destRect.Width;
                    if (sourceRect.Height > destRect.Height)
                        sourceRect.Height = destRect.Height;
                    //sourceRect.Intersect(clientRectangle); // make sure the source is no bigger than the control.
                    return;

                case ImageSizeMode.StretchImage:
                    // The source is always the bounds of them image for StretchImage.
                    // The dest is always the size of the control for StretchImage.
                    return;

                case ImageSizeMode.CenterImage:
                    destRect.X += (clientRectangle.Width - image.Width) / 2;
                    destRect.Y += (clientRectangle.Height - image.Height) / 2;
                    destRect.Size = image.Size;
                    destRect.Intersect(clientRectangle); // make sure the destination is no bigger than the control.

                    // Adjust source if the image is larger than the draw area.
                    if (image.Width > clientRectangle.Width)
                    {
                        sourceRect.X = (image.Width - clientRectangle.Width) / 2;
                        sourceRect.Width = clientRectangle.Width;
                    }
                    if (image.Height > clientRectangle.Height)
                    {
                        sourceRect.Y = (image.Height - clientRectangle.Height) / 2;
                        sourceRect.Height = clientRectangle.Height;
                    }
                    return;

                case ImageSizeMode.Zoom:
                    {
                        // The source is always the bounds of them image for Zoom.
                        // The dest is a proportional rectangle that fits inside the control.
                        Size size = image.Size;
                        float num = Math.Min((float)(((float)clientRectangle.Width) / ((float)size.Width)), (float)(((float)clientRectangle.Height) / ((float)size.Height)));
                        destRect.Width = (int)(float)(size.Width * num); // I need the weird float cast here so the rounding works right.
                        destRect.Height = (int)(float)(size.Height * num); // I need the weird float cast here so the rounding works right.
                        destRect.X += (clientRectangle.Width - destRect.Width) / 2;
                        destRect.Y += (clientRectangle.Height - destRect.Height) / 2;
                        return;
                    }
            }
        }

        //public static Rectangle ImageRectangleFromSizeMode(Image image, Rectangle clientRectangle, ImageSizeMode mode)
        //{
        //    // Based on code from Reflector PictureBox code.
        //    Rectangle rectangle = clientRectangle;
        //    if (image != null)
        //    {
        //        switch (mode)
        //        {
        //            case ImageSizeMode.Normal:
        //            case ImageSizeMode.AutoSize:
        //                rectangle.Size = image.Size;
        //                return rectangle;

        //            case ImageSizeMode.StretchImage:
        //                return rectangle;

        //            case ImageSizeMode.CenterImage:
        //                rectangle.X += (clientRectangle.Width - image.Width) / 2;
        //                rectangle.Y += (clientRectangle.Height - image.Height) / 2;
        //                rectangle.Size = image.Size;
        //                return rectangle;

        //            case ImageSizeMode.Zoom:
        //                {
        //                    Size size = image.Size;
        //                    float num = Math.Min((float)(((float)clientRectangle.Width) / ((float)size.Width)), (float)(((float)clientRectangle.Height) / ((float)size.Height)));
        //                    rectangle.Width = (int)(size.Width * num);
        //                    rectangle.Height = (int)(size.Height * num);
        //                    rectangle.X += (clientRectangle.Width - rectangle.Width) / 2;
        //                    rectangle.Y += (clientRectangle.Height - rectangle.Height) / 2;
        //                    return rectangle;
        //                }
        //        }
        //    }
        //    return rectangle;
        //}

    }
}
